<template>
	<div class="export-dialog-view" v-loading="pageLoading">
		<div class="export-dialog">
			<div class="left-lay">
				<div class="split-title">{{ title }}</div>
				<div class="left-line">
					<div class="line-title">
						{{ $t("message.bhView.leftComponents.device") }}
					</div>
					<div class="line-value">{{ $store.state.plantName }}</div>
				</div>

				<div class="left-line">
					<div class="line-title">
						{{ $t("message.bhView.leftComponents.object") }}
					</div>
					<div class="line-value">
						{{
							$store.state.macType == 1
								? $t("message.bhView.leftComponents.machine")
								: $t("message.bhView.leftComponents.piping")
						}}
					</div>
				</div>

				<div class="left-line">
					<div class="line-title">
						{{ $t("message.reportFrom.exportDialog.title_c") }}
					</div>
					<div class="line-value">{{ fileTypeStr }}</div>
				</div>



				<div class="left-draw" id="left-draw"></div>
			</div>

			<div class="tip-lay"></div>

			<div class="right-lay">
				<div class="split-title text-right">
					<i class="el-icon-close" @click="CloseExprotDiloag"></i>
				</div>
				<div class="split-title">
					{{ $t("message.reportFrom.exportDialog.title_l") }}
				</div>
				<div class="right-line">
					<div class="line-title">
						{{ $t("message.ReportFormTable.D_B.lab1") }}
					</div>
					<div class="line-content">
						<el-date-picker v-model="StartDate" type="month" :format="format" :value-format="format"
							placeholder=" " class="ipt picker">
						</el-date-picker>
						<span>~</span>
						<el-date-picker v-model="EndDate" type="month" :format="format" :value-format="format"
							placeholder=" " class="ipt picker orange">
						</el-date-picker>
					</div>
				</div>

				<div class="right-line">
					<div class="line-title">
						{{ $t("message.ReportFormTable.D_B.lab2") }}
					</div>
					<div class="line-content">
						<el-select v-model="dpsType" placeholder="" class="ipt picker">
							<el-option v-for="(item, index) in dpsTypeList" :key="index" :label="item" :value="index">
							</el-option>
						</el-select>
					</div>
				</div>

				<!-- <div class="right-line">
					<div class="line-title">
						{{ $t("message.ReportFormTable.D_B.lab3") }}111111
					</div>
					<div class="line-content">
						<el-select v-model="distinguish" placeholder="" class="ipt picker">
							<el-option v-for="(item, index) in distinguishList" :key="index" :label="item.label"
								:value="item.key">
							</el-option>
						</el-select>
					</div>
				</div> -->

				<div class="right-line">
					<div class="line-title">
						{{ $t("message.ReportFormTable.D_B.lab4") }}
					</div>
					<div class="line-content">
						<el-select v-model="exeTim" placeholder="" class="ipt picker">
							<el-option v-for="(item, index) in exeTimList" :key="index" :label="item" :value="index">
							</el-option>
						</el-select>
					</div>
				</div>

				<div class="right-line">
					<div class="line-title">
						{{ $t("message.ReportFormTable.D_B.lab5") }}
					</div>
					<div class="line-content" style="padding-left:12px;">
						<el-radio-group v-model="PrintType">
							<el-radio :label="1">{{ $t("message.ReportFormTable.D_B.lab6") }}</el-radio>
							<el-radio :label="2">{{ $t("message.ReportFormTable.D_B.lab7") }}</el-radio>
						</el-radio-group>
					</div>
				</div>





			</div>
			<footer class="footer-view">
				<div class="">
					<div class="line-title" style="width: 100%">
						{{ $t("message.reportFrom.exportDialog.tip") }}
					</div>
				</div>
				<div>
					<el-button @click="CloseExprotDiloag">Cancel</el-button>
					<el-button type="primary" @click="onExportClick">OK</el-button>
				</div>
			</footer>
		</div>
	</div>
</template>

<script>
import {
	ExResOfferDetialsReportVerify,
	exResOfferDetialsReport,
	getOfferDetialCombox,

	CollectFiles,
} from "@/api/api";
import * as svg_opt from "@/utils/svg_opt.js";
import { getLocalStorage } from "@/utils/storage";

export default {
	props: ["title", "fileTypeStr", "selectMacData", "fileRadio"],
	name: "D_B_Export",
	data() {
		return {
			pageLoading: false,
			format: 'yyyy-MM',
			StartDate: '',
			EndDate: '',
			PrintType: 1,

			exeTim: '',
			exeTimList: [],

			dpsType: '',
			dpsTypeList: [],
			distinguishList: [],
			distinguish: '',

		};
	},
	components: {},
	watch: {},
	methods: {
		async onExportClick() {
			this.pageLoading = true;
			let MacNumb = []
			for (let i = 0; i < this.selectMacData.length; i++) {
				MacNumb.push(this.selectMacData[i].macName)
			}

			let dataJson = {
				PltCode: this.selectMacData[0].pltCode,
				MacType: this.selectMacData[0].macType,
				MacNumb,
				StartDate: this.StartDate || '',
				EndDate: this.EndDate || '',
				PrintType: this.PrintType,
				ActuKbn: isNaN(this.distinguish) || this.distinguish == ' ' ? 0 : this.distinguish,
				DpsType: isNaN(this.dpsType) || this.dpsType == ' ' ? 0 : this.dpsType,
				ExeTim: isNaN(this.exeTim) || this.exeTim == ' ' ? 0 : this.exeTim
			}
			let rr = await ExResOfferDetialsReportVerify(dataJson);
			if (rr.data.code == 0) {
				this.pageLoading = false;
				this.$alert(rr.data.data, ' ', {
					confirmButtonText: 'OK',
					type: 'warning',
				});
				return
			}
			if (!rr) {
				this.pageLoading = false;
				return
			}

			let res = await exResOfferDetialsReport(dataJson);
			this.pageLoading = false;
			if (!res) {
				return
			}
			this.$emit("CloseExprotDiloag");
			let noDataStr = res.data.msg;
			if (noDataStr && this.PrintType == 2 && res.data.code == 2) {
				if (getLocalStorage("lang") == 0) {
					this.$alert(`${noDataStr}<p style="color:red;">${this.$t('message.public.tip5')}</p>`, '', {
						confirmButtonText: 'OK',
						type: 'warning',
						dangerouslyUseHTMLString: true,
					});
				} else {

					this.$alert(`${noDataStr}<p style="color:red;">${this.$t('message.public.tip5')}</p>`, '', {
						confirmButtonText: 'OK',
						type: 'warning',
						dangerouslyUseHTMLString: true,
					});
				}
			}
			if (res.data.data) {
				window.open(res.data.data)
			}



			// if (this.PrintType == 2) {
			// 	this.choesTwo();
			// } else {
			// 	this.choesOne();
			// }
		},
		async choesOne() {
			this.pageLoading = true;
			let mscNameList = []
			for (let i = 0; i < this.selectMacData.length; i++) {
				mscNameList.push(this.selectMacData[i].macName)
			}
			let res = await this.exResOfferDetialsReportApi(0, mscNameList);
			this.pageLoading = false;

			if (res == 'flg_NO') {
				return
			}
			if (res.data.code != 0) {
				console.log("s5fd1");
				this.$alert(res.data.msg, '', {
					confirmButtonText: 'OK',
					type: 'warning',
					callback: action => {
					}
				})
				return
			}
			this.$emit("CloseExprotDiloag");
			window.open(res.data.data)
		},
		async choesTwo() {
			this.pageLoading = true;
			let pfdList = []
			let noDataList = []
			for (let i = 0; i < this.selectMacData.length; i++) {
				let temp = await this.exResOfferDetialsReportApi(i, [this.selectMacData[i].macName]);
				if (temp == 'flg_NO') {
					this.pageLoading = false;
					return
				}
				if (temp.data.code == 0) pfdList.push(temp.data.data)
				if (temp.data.code == -310) noDataList.push(this.selectMacData[i].macName)
				if (temp.data.code == -300) {

					this.$alert(temp.data.msg, '', {
						confirmButtonText: 'OK',
						type: 'warning',
					});
				}
				console.log(temp.data.code)
			}

			if (noDataList.length != 0) {
				if (getLocalStorage("lang") == 0) {
					this.$alert(`${noDataList}<p style="color:red;">上記の機器は出力されませんでした。</p>`, '', {
						confirmButtonText: 'OK',
						type: 'warning',
						dangerouslyUseHTMLString: true,
					});
				} else {
					this.$alert(this.$store.state.macType == 1 ? `${noDataList} <p style="color:red;">There are total ${noDataList.length} Equipments with no date.</p>`
						: `${noDataList} <p style="color:red;">There are total ${noDataList.length} Spools  with no date.</p>`, '', {
						confirmButtonText: 'OK',
						type: 'warning',
						dangerouslyUseHTMLString: true,
					});
				}
			}

			console.log(pfdList)
			this.pageLoading = false;
			if (pfdList.length == 0) {
				return
			} else if (pfdList.length == 1) {
				window.open(pfdList[0]);
				this.$emit("CloseExprotDiloag");
			} else {
				this.pageLoading = true;
				let zipRe = await CollectFiles({
					pdfList: pfdList,
				}).catch((err) => {
					console.log(err)
					return {
						data: {
							code: -1,
						},
					};
				});

				if (zipRe.data.code == 0) {
					this.$emit("CloseExprotDiloag");
					window.open(zipRe.data.data);
					this.pageLoading = false;
				} else {

					this.$alert(zipRe.data.msg, '', {
						confirmButtonText: 'OK',
						type: 'warning',
					});
				}
				this.pageLoading = false;
			}
		},

		async exResOfferDetialsReportApi(index, MacNumb) {
			let dataJson = {
				PltCode: this.selectMacData[index].pltCode,
				MacType: this.selectMacData[index].macType,
				MacNumb,
				StartDate: this.StartDate || '',
				EndDate: this.EndDate || '',
				PrintType: this.PrintType,
				ActuKbn: isNaN(this.distinguish) || this.distinguish == ' ' ? 0 : this.distinguish,
				DpsType: isNaN(this.dpsType) || this.dpsType == ' ' ? 0 : this.dpsType,
				ExeTim: isNaN(this.exeTim) || this.exeTim == ' ' ? 0 : this.exeTim
			}
			let rr = await ExResOfferDetialsReportVerify(dataJson);
			if (rr.data.code == 0) {
				this.$alert(rr.data.msg, ' ', {
					confirmButtonText: 'OK',
					type: 'warning',
				});

				return 'flg_NO'
			}

			let res = await exResOfferDetialsReport(dataJson);
			return res
		},





		CloseExprotDiloag() {
			this.$emit("CloseExprotDiloag");
		},
		inData() {
			this.getOfferDetialComboxApi();
		},
		async getOfferDetialComboxApi() {
			let dataJson = {
				dmgCode: this.$store.state.dmgCode,
				macCode: this.$store.state.macCode,
				pltCode: this.$store.state.pltCode,
				rnCode: this.$store.state.prnCode || 0,
				userName: getLocalStorage("userName"),
			}
			let res = await getOfferDetialCombox(dataJson);
			console.log(res)
			this.dpsTypeList = res.data.data.dpsTypeList;
			this.dpsType = res.data.data.dpsTypeList[0];
			this.distinguishList = res.data.data.distinguish;
			this.distinguishList.unshift({ key: -1, label: ' ' })
			this.distinguish = res.data.data.distinguish[0].label;

			this.exeTimList = res.data.data.exeTimList;
			this.exeTim = res.data.data.exeTimList[0];

			this.format = res.data.data.dateFormat;
			this.StartDate = res.data.data.periodDataList[0]
			this.EndDate = res.data.data.periodDataList[1]
		},
	},
	created() {
		this.inData();
	},
	mounted() { },
	beforeDestroy() { },
	destroyed() { },
};
</script>
<style lang="scss" scoped>
.export-dialog-view {
	position: fixed;
	width: 100%;
	height: 100%;
	left: 0;
	top: 0;
	z-index: 6;
}

.export-dialog {
	position: absolute;
	top: 50%;
	left: 50%;
	transform: translate(-50%, -50%);
	width: 912px;
	height: 415px;
	// height: 292px;
	background: #ffffff;
	box-shadow: 0px 12px 48px 16px rgba(0, 0, 0, 0.05),
		0px 9px 28px 0px rgba(0, 0, 0, 0.08),
		0px 6px 16px -8px rgba(0, 0, 0, 0.15);
	display: flex;
	flex-direction: row;
	padding: 0 12px;
}

.left-lay {
	position: relative;
	width: 40%;
	height: 100%;
	// border-right: 1px solid #ccc;
	display: flex;
	flex-direction: column;
	// background-color: blanchedalmond;

	.left-line {
		width: 100%;
		display: flex;
		flex-direction: row;
		align-items: center;
		padding: 0px 20px;
		padding-top: 10px;

		.line-title {
			font-size: 14px;
			font-weight: 700;
			color: rgba(0, 0, 0, 0.85);
			line-height: 22px;
		}

		.line-value {
			font-size: 14px;
			font-weight: 400;
			color: #7b7b7b;
			line-height: 22px;
			margin-left: 10px;
		}
	}

	.left-draw {
		position: absolute;
		top: 100vh;
		width: 750px;
		height: 611px;
		// flex-grow: 1;
		// flex-shrink: 1;
		background-color: bisque;
	}
}

.tip-lay {
	background: red;
	width: 7px;
	border-radius: 4px;
	background: #ccc;
	margin-top: 46px;
	margin: 46px 15px 85px 15px;
}

.right-lay {
	position: relative;
	width: 70%;
	height: 100%;
	display: flex;
	flex-direction: column;

	.el-icon-close {
		font-size: 16px;
		cursor: pointer;
		width: 46px;
		height: 45px;
		line-height: 45px;
	}

	.right-block-container {
		width: 100%;
		display: flex;
		flex-direction: column;
	}

	.text-right {
		padding: 0 !important;
		text-align: right;
	}

	.right-line {
		width: 100%;
		display: flex;
		flex-direction: row;
		align-items: center;
		padding: 0px 12px;
		padding-top: 17px;

		.line-title {
			font-size: 14px;
			font-weight: 700;
			color: rgba(0, 0, 0, 0.85);
			line-height: 22px;
			text-align: left;
			margin-right: 10px;
			width: 110px;
		}

		.line-content {
			flex-grow: 1;
			flex-shrink: 1;
			display: flex;
			flex-direction: row;
			align-items: center;
		}

		.ipt {
			//     width: 368px;
			width: 100%;
			height: 32px;

			::v-deep .el-input__inner {
				width: 100%;
				height: 32px;
			}

			::v-deep .el-input__inner:focus {
				border: 1px solid #dcdfe6;
			}

			::v-deep .el-input__icon {
				line-height: 32px;
				color: #909399;
			}

			::v-deep .el-input__suffix {
				width: 32px;
				right: 0;
				background: #dee0e3;
			}
		}

		.picker {
			width: 130px;
			margin: 0px 10px;

			::v-deep .el-input__inner {
				width: 130px;
			}

			::v-deep .el-input__suffix {
				background: transparent;
			}
		}

		.orange {
			::v-deep .el-input__inner {
				// width: 130px;
				background-color: #FFDEACFF;
			}
		}

		.divider {
			width: 100%;
			height: 1px;
			background-color: #f0f0f0;
		}
	}

	.right-line.bottom-border {
		padding-bottom: 10px;
		// border-bottom: 1px solid #f0f0f0;
	}

	.flex-row {
		flex-direction: row;
	}

	.flex-column {
		flex-direction: column;
	}
}

.split-title {
	width: 100%;
	font-size: 16px;
	font-weight: 700;
	color: rgba(0, 0, 0, 0.85);
	line-height: 22px;
	padding: 12px 12px;
	border-bottom: 1px solid #f0f0f0;
}

.footer-view {
	width: calc(100% - 30px);
	box-sizing: border-box;
	position: absolute;
	z-index: 10;
	bottom: 16px;
	// padding: 0 15px;
	display: flex;
	flex-direction: row;
	align-items: center;
	justify-content: space-between;
	// background-color: red;
	padding-top: 17px;
	border-top: 1px solid #f0f0f0;
	font-size: 16px;
	font-weight: 700;

	button {
		width: 82px !important;
		height: 32px;
		// background: #ffffff;
		// border: 1px solid #d9d9d9;
		margin-left: 12px;
		border-radius: 4px;
		font-size: 14px;
		font-weight: 500;
		line-height: 0;
	}

	button:nth-child(2) {
		// background: #106ebe;
		// color: #fff;
	}
}

.p0 {
	padding: 12px 0;
}
</style>
